package com.example.myapplication.fragment

import android.util.Log
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.Observer
import androidx.navigation.NavOptions
import androidx.navigation.fragment.findNavController
import androidx.recyclerview.widget.LinearLayoutManager
import com.bumptech.glide.Glide
import com.drake.brv.utils.linear
import com.drake.brv.utils.models
import com.drake.brv.utils.setup
import com.example.myapplication.MainViewModel
import com.example.myapplication.R
import com.example.myapplication.databinding.FragmentHomeBinding
import com.example.myapplication.databinding.RewardItemLayoutBinding
import com.example.myapplication.db.Reward
import com.example.myapplication.db.User
import com.example.myapplication.type.SortOrder
import com.example.myapplication.type.SortType
import com.example.myapplication.utils.DivisionString
import com.example.myapplication.utils.TimeUtils

class HomeFragment : BaseFragment<FragmentHomeBinding>() {
    private val mViewModel: MainViewModel by activityViewModels()
    override fun initBinding(): FragmentHomeBinding {
        return FragmentHomeBinding.inflate(layoutInflater)
    }

    override fun initView() {
        mViewModel.userList.observe(viewLifecycleOwner, Observer { users ->
            if (users != null) {
                Log.d("LoginFragment", "Observed users data: $users")
            } else {
                Log.e("LoginFragment", "User list is null")
            }
            mBinding.awardRecycleView.models= mViewModel.getAllRewardByTimeOrPrice(SortType.ByTime,SortOrder.ReverseOrder)
        })
        var thisAccount=""
        mViewModel.Logged_user.observe(viewLifecycleOwner,Observer{
            thisAccount=it
        })
        val timeUtils=TimeUtils()
        mBinding.awardRecycleView.layoutManager = LinearLayoutManager(context)
        mBinding.awardRecycleView.linear().setup {
            addType<Reward>(R.layout.reward_item_layout)
            onBind {
                val binding = getBinding<RewardItemLayoutBinding>()
                val model = getModel<Reward>()
                Glide.with(context)
                    .load(R.drawable.user)
                    .into(binding.ivIcon)
                binding.tvTitle.text = model.account
                binding.tvCountry.text = model.content
                binding.time.text=timeUtils.splitDateTime(model.time)
                binding.price.text=model.price.toString()+"￥"
                itemView.setOnClickListener {
                    mViewModel.findUserByAccount(thisAccount).also {
                        mViewModel.thisRewardContext.value=model
                    }
                    findNavController().navigate(R.id.action_homeFragment_to_rewardFragment)
                }
            }
        }
        mBinding.awardRecycleView.models= mViewModel.getAllRewardByTimeOrPrice(SortType.ByTime,SortOrder.ReverseOrder)
        mBinding.AddReward.setOnClickListener {

            Log.d("HOmeFragment","我被点击了")
            findNavController().navigate(R.id.action_homeFragment_to_addRewardFragment)

        }
        mBinding.myApplyRewardList.setOnClickListener {
            findNavController().navigate(R.id.action_homeFragment_to_myApplyRewardFragment)
        }
        mBinding.myRewardList.setOnClickListener {
            findNavController().navigate(R.id.action_homeFragment_to_myRewardFragment)
        }
    }
}
